<template>
  <div class="app-container">
    <el-button type="primary"
               size="small"
               @click="handleAdd">新 增</el-button>
    <el-table :data="trialList"
              border
              v-loading="loading">
      <el-table-column label="试验名称"
                       align="center"
                       prop="trialName"
                       :show-overflow-tooltip="true"
                       min-width="180"></el-table-column>
      <el-table-column label="试验人"
                       align="center"
                       prop="createBy"
                       :show-overflow-tooltip="true"
                       min-width="180"></el-table-column>
      <el-table-column label="创建时间"
                       align="center"
                       prop="createTime"
                       :show-overflow-tooltip="true"
                       min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态"
                       align="center"
                       prop="trialStatus"
                       :show-overflow-tooltip="true"
                       min-width="180">
        <template slot-scope="scope">
          <span>{{trialStatusMap[scope.row.trialStatus]}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       align="center"
                       min-width="120">
        <template slot-scope="scope">
          <el-button size="mini"
                     icon="el-icon-edit"
                     :disabled="scope.row.trialStatus != '0'"
                     @click="editTrialProcess(scope.row)"
                     type="text">编 辑</el-button>
          <el-button type="text"
                     size="mini"
                     @click="viewProcessLog(scope.row)"
                     icon="el-icon-pie-chart">查看日志</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <process-log ref="processLog"></process-log>
  </div>
</template>

<script>
import { getTrialProcessList, startTrialProcess, getTrialProcessLog } from '@/api/trial/trial-list'
import ProcessLog from './components/process-log.vue'
export default {
  components: { ProcessLog },
  data () {
    return {
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      trialList: [],
      trialStatusMap: {
        '0': '未提交',
        '1': '审批中',
        '2': '进行中',
        '3': '已结束'
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      getTrialProcessList(this.queryParams).then(res => {
        this.trialList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    editTrialProcess (row) {
      let param = Buffer.from(JSON.stringify({
        category: "1682268954078318594",
        deploymentId: "679b4725-2a94-11ee-8b90-422006a01095",
        formKey: "wf_ex_trial",
        processId: "68cc2608-2a94-11ee-8b90-422006a01095",
        key: "process_FbhhK4wzPD4KRbnsFninfyKEdCc7YEs8",
        name: encodeURI("试验仿真流程"),
        status: 1,
        id: row.id
      })).toString('base64')
      this.$router.push(`/workflow/process/external/trial/start?p=${param}`)
    },
    handleAdd () {
      let param = Buffer.from(JSON.stringify({
        category: "1682268954078318594",
        deploymentId: "679b4725-2a94-11ee-8b90-422006a01095",
        formKey: "wf_ex_trial",
        processId: "68cc2608-2a94-11ee-8b90-422006a01095",
        key: "process_FbhhK4wzPD4KRbnsFninfyKEdCc7YEs8",
        name: encodeURI("试验仿真流程"),
        status: 1
      })).toString('base64')
      this.$router.push(`/workflow/process/external/trial/start?p=${param}`)
    },
    viewProcessLog (row) {
      this.$refs.processLog.open(row.trialFlowId, row.processInsId)
    },
  }
}
</script>

<style>
</style>